package com.wonders.common.web.listener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.stereotype.Component;

import com.wonders.common.config.manager.ConfigManager;
import com.wonders.framework.web.context.GlobalContext;

/**
 * spring容器事件监听器
 * 
 * @author WuBin
 * 
 */
@Component
public class ApplicationEventListener implements ApplicationListener {

	@Autowired
	private ConfigManager configManager;

	public void onApplicationEvent(ApplicationEvent arg0) {
		if (arg0 instanceof ContextClosedEvent) {
			configManager.destroy();
		} else if (arg0 instanceof ContextRefreshedEvent) {
			ContextRefreshedEvent contextRefreshedEvent = (ContextRefreshedEvent) arg0;
			ApplicationContext applicationContext = contextRefreshedEvent
					.getApplicationContext();
			GlobalContext.setApplicationContext(applicationContext);
			configManager.init();
		} else if (arg0 instanceof ContextStartedEvent) {

		} else if (arg0 instanceof ContextStoppedEvent) {

		} else {

		}
	}

}
